﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.IServices.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Common.HttpContextUser;
using AutoMapper;
using PeiYue.Model.Dto;
using System.Collections.Generic;
using System.Linq;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Common.Helper;
using Microsoft.AspNetCore.Authorization;

namespace PeiYue.Api.Controllers.Tms
{
    /// <summary>
    /// 车辆管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class VehicleController : BaseController<VehicleDao>
    {
        readonly IVehicleServices _vehicleServices;
        readonly IRouterServices _routerServices;
        readonly IUser _user;
        IMapper _mapper;
        public VehicleController(IVehicleServices vehicleServices, IRouterServices routerServices, IUser user, IMapper mapper):base(vehicleServices,user)
        {
            _vehicleServices = vehicleServices;
            _routerServices = routerServices;
            _user = user;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取车辆详情
        /// </summary>
        /// <param name="id">车辆id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<SearchVehicleResponseDto>> Get(long id)
        {
            return new MessageModel<SearchVehicleResponseDto>()
            {
                msg = "获取成功",
                success = true,
                response = await _vehicleServices.GetById(id)
            };
        }
        /// <summary>
        /// 获取车辆分页列表
        /// </summary>
        /// <param name="requestDto">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<SearchVehicleResponseDto>>> GetList([FromQuery]SearchVehicleRequestDto requestDto)
        {
            var data = await _vehicleServices.GetPage(requestDto);
            return new MessageModel<PageModel<SearchVehicleResponseDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }
        /// <summary>
        /// 新增车辆
        /// </summary>
        /// <param name="vehicleDto">新增车辆参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] UpdateVehicleDto vehicleDto)
        {
            var data = new MessageModel<string>();
            var list = await _vehicleServices.Query(t => t.rt_id == vehicleDto.rt_id && t.status == (int)CommonEnum.Status.Enable);
            if (list.Count > 0)
            {
                var routerDao = await _routerServices.QueryById(vehicleDto.rt_id);
                data.msg = $"[{routerDao?.names}]线路已经关联了车辆，一条线路只能关联一辆车";
                data.status = 500;
                data.success = false;
                return data;
            }
            var vehicleDao = _mapper.Map<VehicleDao>(vehicleDto);
            vehicleDao.load_amt = StringHelper.parsePrice(vehicleDto.load_amt_text);
            vehicleDao.create_user = _user.ID;
            vehicleDao.update_user = _user.ID;
            vehicleDao.id = await _vehicleServices.GenId();
            await _vehicleServices.Add(vehicleDao);
            if (vehicleDao.id > 0)
            {
                data.success = true;
                data.msg = "添加成功";
            }
            return data;
        }
        /// <summary>
        /// 修改车辆
        /// </summary>
        /// <param name="vehicleDto">修改车辆参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Update([FromBody] UpdateVehicleDto vehicleDto)
        {
            var data = new MessageModel<string>();
            var vehicleDao = _mapper.Map<VehicleDao>(vehicleDto);
            vehicleDao.load_amt = StringHelper.parsePrice(vehicleDto.load_amt_text);
            if (vehicleDao != null && vehicleDao.id > 0)
            {
                var list = await _vehicleServices.Query(t =>t.id!=vehicleDao.id && t.rt_id == vehicleDto.rt_id && t.status == (int)CommonEnum.Status.Enable);
                if (list.Count > 0)
                {
                    var routerDao = await _routerServices.QueryById(vehicleDto.rt_id);
                    data.msg = $"[{routerDao?.names}]线路已经关联了车辆，一条线路只能关联一辆车";
                    data.status = 500;
                    data.success = false;
                    return data;
                }
                vehicleDao.update_user = _user.ID;
                data.success=await _vehicleServices.Update(vehicleDao,null,new List<string> { "status", "create_time", "create_user" });
                if (data.success)
                {
                    data.success = true;
                    data.msg = "更新成功";
                }
            }
            return data;
        }
        /// <summary>
        /// 获取车辆下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<List<SelectItemDto>>> GetSelectItem()
        {
            var data = await _vehicleServices.Query(p => p.status == (int)CommonEnum.Status.Enable);
            var result = data.Select(p => new SelectItemDto { Id = p.id, Name = p.names }).ToList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = result
            };
        }

    }
}